package com.fr.chart.core.glyph;

import com.fr.base.core.json.JSONException;
import com.fr.base.core.json.JSONObject;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLableReader;
import com.fr.chart.GeneralInfo;
import com.fr.chart.axis.DataLabelInfo;
import com.fr.chart.axis.SeriesAttrContents;
import com.fr.chart.axis.SeriesAttrPosition;
import com.fr.chart.axis.SeriesAttrStyle;
import com.fr.chart.axis.TextAttr;
import com.fr.chart.plot.DataPoint;
import com.fr.chart.plot.DataSeries;
import com.fr.chart.plot.SeriesCollection;
import com.fr.chart.shape3d.Cylinder;
import java.awt.Graphics;
import java.awt.Shape;
import java.awt.geom.Arc2D;
import java.awt.geom.Arc2D.Double;
import java.awt.geom.Dimension2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Point2D;
import java.awt.geom.Point2D.Double;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Rectangle2D.Double;

public class PiePlotGlyph extends PlotGlyph
{
  private static final long serialVersionUID = 7862387644048932954L;
  public static final String XML_TAG = "PiePlotGlyph";
  private int startAngle = 100;
  private int separatePercent = 0;
  protected transient int oneNumber;
  protected transient int twoNumber;
  protected transient int threeNumber;
  protected transient int fourNumber;

  public void layoutAxisGlyph()
  {
  }

  public void layoutDataSeriesGlyph()
  {
    for (int i = 0; i < getCategoryCount(); ++i)
    {
      instancePieNumber();
      double d1 = getPieDimension();
      double d2 = d1 / (2.0D + 2 * getSeparatePercent() / 100.0D);
      double d3 = getStartAngle();
      for (int j = 0; j < getSeriesSize(); ++j)
      {
        DataSeries localDataSeries = getSeries(j);
        DataPoint localDataPoint = localDataSeries.getDataPoint(i);
        if (localDataPoint.isValueIsNull())
          break label251:
        double d4 = (i + 0.5D) * getBounds().getWidth() / getCategoryCount();
        Point2D.Double localDouble = new Point2D.Double(d4, getBounds().getHeight() / 2.0D);
        Arc2D localArc2D = getDataPointShape(localDouble, j, i, d3, d2);
        ShapeGlyph localShapeGlyph = new ShapeGlyph(localArc2D);
        localShapeGlyph.getInfo().dealCondition(getSeriesCollection(), j);
        localDataPoint.setDrawImpl(localShapeGlyph);
        TextGlyph localTextGlyph = localDataPoint.getDataLabel();
        double d5 = getHalfAngle(d3, j, i);
        dealLabelBounds(localDataPoint.getShape().getBounds2D(), localDouble, d5, j, i, localTextGlyph, d1 * 0.5D);
        dealLeadLine(localArc2D, localDataPoint, localTextGlyph, j, d5);
        d3 += getPercent(j, i) * 360.0D;
      }
    }
    label251:
  }

  protected void instancePieNumber()
  {
    this.oneNumber = 0;
    this.twoNumber = 0;
    this.threeNumber = 0;
    this.fourNumber = 0;
    for (int i = 0; i < getCategoryCount(); ++i)
    {
      double d1 = getStartAngle();
      for (int j = 0; j < getSeriesSize(); ++j)
      {
        DataSeries localDataSeries = getSeries(j);
        DataPoint localDataPoint = localDataSeries.getDataPoint(i);
        SeriesAttrPosition localSeriesAttrPosition = (SeriesAttrPosition)getSeriesCollection().getSeriesCondition(new SeriesAttrPosition(), j);
        if ((localSeriesAttrPosition.getPosition() == 6) && (localDataPoint.getDataLabel() != null))
        {
          TextGlyph localTextGlyph = localDataPoint.getDataLabel();
          if ((localTextGlyph.getText() != null) && (localTextGlyph.getText().length() > 0))
          {
            double d2 = getHalfAngle(d1, j, i);
            double d3 = d2 % 360.0D;
            if ((d3 <= 90.0D) && (d3 > 0.0D))
              this.oneNumber += 1;
            else if ((d3 <= 180.0D) && (d3 > 90.0D))
              this.twoNumber += 1;
            else if ((d3 <= 270.0D) && (d3 > 180.0D))
              this.threeNumber += 1;
            else if ((d3 <= 360.0D) && (d3 > 270.0D))
              this.fourNumber += 1;
          }
        }
        d1 += getPercent(j, i) * 360.0D;
      }
    }
  }

  protected double getPieDimension()
  {
    Rectangle2D localRectangle2D = getBounds();
    double d = Math.min(localRectangle2D.getWidth() / getCategoryCount(), localRectangle2D.getHeight());
    d = deal4BestRad(d);
    return d;
  }

  protected double deal4BestRad(double paramDouble)
  {
    for (int i = 0; i < getSeriesSize(); ++i)
    {
      DataLabelInfo localDataLabelInfo = new DataLabelInfo();
      localDataLabelInfo.dealCondition(getSeriesCollection(), i);
      if ((localDataLabelInfo != null) && (localDataLabelInfo.getPosition() == 6))
      {
        paramDouble = (paramDouble - 28.0D) * 0.6D;
        return paramDouble;
      }
    }
    return paramDouble;
  }

  private Arc2D getDataPointShape(Point2D paramPoint2D, int paramInt1, int paramInt2, double paramDouble1, double paramDouble2)
  {
    double d1 = getSeparatePercent() / 100.0D * paramDouble2;
    double d2 = getHalfAngle(paramDouble1, paramInt1, paramInt2);
    Arc2D.Double localDouble = new Arc2D.Double(paramPoint2D.getX() + d1 * Math.cos(Math.toRadians(d2)) - paramDouble2, paramPoint2D.getY() - d1 * Math.sin(Math.toRadians(d2)) - paramDouble2, 2.0D * paramDouble2, 2.0D * paramDouble2, paramDouble1, getPercent(paramInt1, paramInt2) * 360.0D, 2);
    return localDouble;
  }

  protected void dealLeadLine(Arc2D paramArc2D, DataPoint paramDataPoint, TextGlyph paramTextGlyph, int paramInt, double paramDouble)
  {
    SeriesAttrContents localSeriesAttrContents = (SeriesAttrContents)getSeriesCollection().getSeriesCondition(new SeriesAttrContents(), paramInt);
    SeriesAttrPosition localSeriesAttrPosition = (SeriesAttrPosition)getSeriesCollection().getSeriesCondition(new SeriesAttrPosition(), paramInt);
    String str = paramTextGlyph.getText();
    if ((str != null) && (localSeriesAttrContents.isShowGuidLine()) && (paramTextGlyph.getBounds() != null) && (localSeriesAttrPosition.getPosition() == 6))
    {
      Rectangle2D localRectangle2D = paramTextGlyph.getBounds();
      Point2D localPoint2D = Cylinder.getArcPoint(paramArc2D, paramDouble);
      double d1 = localPoint2D.getX();
      double d2 = localPoint2D.getY();
      double d3 = localRectangle2D.getX();
      double d4 = localRectangle2D.getY();
      double d5 = 0.0D;
      double d6 = 0.0D;
      paramDouble %= 360.0D;
      if ((paramDouble >= 0.0D) && (paramDouble < 90.0D))
      {
        d5 = localRectangle2D.getX() - 4.0D;
        d6 = localRectangle2D.getY() + localRectangle2D.getHeight() / 2.0D;
        d3 = localRectangle2D.getX();
        d4 = localRectangle2D.getY() + localRectangle2D.getHeight() / 2.0D;
      }
      else if ((paramDouble >= 90.0D) && (paramDouble < 180.0D))
      {
        d5 = d3 + localRectangle2D.getWidth() + 4.0D;
        d6 = d4 + localRectangle2D.getHeight() / 2.0D;
        d3 = localRectangle2D.getX() + localRectangle2D.getWidth();
        d4 = localRectangle2D.getY() + localRectangle2D.getHeight() / 2.0D;
      }
      else if ((paramDouble >= 180.0D) && (paramDouble < 270.0D))
      {
        d5 = localRectangle2D.getX() + localRectangle2D.getWidth() / 2.0D;
        d6 = localRectangle2D.getY() - 4.0D;
        d3 = localRectangle2D.getX() + localRectangle2D.getWidth() / 2.0D;
        d4 = localRectangle2D.getY();
      }
      else if ((paramDouble >= 270.0D) && (paramDouble < 360.0D))
      {
        d5 = localRectangle2D.getX() - 4.0D;
        d6 = localRectangle2D.getY() + localRectangle2D.getHeight() / 2.0D;
        d3 = localRectangle2D.getX();
        d4 = localRectangle2D.getY() + localRectangle2D.getHeight() / 2.0D;
      }
      GeneralPath localGeneralPath = new GeneralPath();
      localGeneralPath.moveTo((float)d1, (float)d2);
      localGeneralPath.lineTo((float)d5, (float)d6);
      localGeneralPath.lineTo((float)d3, (float)d4);
      paramDataPoint.setLeadLine(localGeneralPath);
    }
  }

  protected void dealLabelBounds(Rectangle2D paramRectangle2D, Point2D paramPoint2D, double paramDouble1, int paramInt1, int paramInt2, TextGlyph paramTextGlyph, double paramDouble2)
  {
    double d1 = paramDouble2 * 1.5D;
    double d2 = paramPoint2D.getX();
    double d3 = paramPoint2D.getY();
    String str = paramTextGlyph.getText();
    if ((str != null) && (str.length() > 0))
    {
      double d4;
      double d5;
      SeriesAttrStyle localSeriesAttrStyle = (SeriesAttrStyle)getSeriesCollection().getSeriesCondition(new SeriesAttrStyle(), paramInt1);
      TextAttr localTextAttr = localSeriesAttrStyle.getTextAttr();
      Dimension2D localDimension2D = TextGlyph.calculateTextDimension(str, localTextAttr);
      SeriesAttrPosition localSeriesAttrPosition = (SeriesAttrPosition)getSeriesCollection().getSeriesCondition(new SeriesAttrPosition(), paramInt1);
      if (localSeriesAttrPosition.getPosition() == 6)
      {
        double d7;
        d4 = paramDouble1 % 360.0D;
        d5 = 0.0D;
        double d6 = 0.0D;
        double d8 = 0.0D;
        if ((d4 <= 90.0D) && (d4 > 0.0D) && (this.oneNumber > 0))
        {
          d7 = getIndexNumber(paramInt2, 1, d4);
          d8 = 90.0D * d7 / (this.oneNumber + 1);
        }
        else if ((d4 <= 180.0D) && (d4 > 90.0D) && (this.twoNumber > 0))
        {
          d7 = getIndexNumber(paramInt2, 2, d4);
          d8 = 90.0D * d7 / (this.twoNumber + 1) + 90.0D;
        }
        else if ((d4 <= 270.0D) && (d4 > 180.0D) && (this.threeNumber > 0))
        {
          d7 = getIndexNumber(paramInt2, 3, d4);
          d8 = 90.0D * d7 / (this.threeNumber + 1) + 180.0D;
        }
        else if ((d4 <= 360.0D) && (d4 > 270.0D) && (this.fourNumber > 0))
        {
          d7 = getIndexNumber(paramInt2, 4, d4);
          d8 = 90.0D * d7 / (this.fourNumber + 1) + 270.0D;
        }
        double d9 = localDimension2D.getHeight();
        double d10 = localDimension2D.getWidth();
        d5 = d2 + d1 * Math.cos(3.141592653589793D * d8 / 180.0D);
        d6 = d3 - d1 * Math.sin(3.141592653589793D * d8 / 180.0D);
        if ((d8 > 90.0D) && (d8 < 270.0D))
          d5 -= d10;
        paramTextGlyph.setBounds(new Rectangle2D.Double(d5, d6, d10, d9));
      }
      else
      {
        d4 = paramRectangle2D.getX() + (paramRectangle2D.getWidth() - localDimension2D.getWidth()) / 2.0D;
        d5 = paramRectangle2D.getY() + (paramRectangle2D.getHeight() - localDimension2D.getHeight()) / 2.0D;
        paramTextGlyph.setBounds(new Rectangle2D.Double(d4, d5, localDimension2D.getWidth(), localDimension2D.getHeight()));
      }
    }
  }

  protected int getIndexNumber(int paramInt1, int paramInt2, double paramDouble)
  {
    int i = 1;
    double d1 = getStartAngle();
    for (int j = 0; j < getSeriesSize(); ++j)
    {
      DataSeries localDataSeries = getSeries(j);
      DataPoint localDataPoint = localDataSeries.getDataPoint(paramInt1);
      SeriesAttrPosition localSeriesAttrPosition = (SeriesAttrPosition)getSeriesCollection().getSeriesCondition(new SeriesAttrPosition(), j);
      if ((localSeriesAttrPosition.getPosition() == 6) && (localDataPoint.getDataLabel() != null))
      {
        TextGlyph localTextGlyph1 = localDataPoint.getDataLabel();
        TextGlyph localTextGlyph2 = (TextGlyph)localTextGlyph1;
        if ((localTextGlyph2.getText() != null) && (localTextGlyph2.getText().length() > 0))
        {
          double d2 = getHalfAngle(d1, j, paramInt1) % 360.0D;
          if ((paramInt2 == 1) && (paramDouble < 90.0D) && (paramDouble > 0.0D))
            if ((paramDouble > d2) && (d2 > 0.0D))
              ++i;
          else if ((paramInt2 == 2) && (paramDouble > 90.0D) && (paramDouble < 180.0D))
            if ((paramDouble > d2) && (d2 > 90.0D))
              ++i;
          else if ((paramInt2 == 3) && (paramDouble > 180.0D) && (paramDouble < 270.0D))
            if ((paramDouble > d2) && (d2 > 180.0D))
              ++i;
          else if ((paramInt2 == 4) && (paramDouble > 270.0D) && (paramDouble < 360.0D) && (paramDouble > d2) && (d2 > 270.0D))
            ++i;
        }
      }
      d1 += getPercent(j, paramInt1) * 360.0D;
    }
    return i;
  }

  protected double getHalfAngle(double paramDouble, int paramInt1, int paramInt2)
  {
    return (paramDouble + 0.5D * getPercent(paramInt1, paramInt2) * 360.0D);
  }

  private double getTotalValue(int paramInt)
  {
    double d = 0.0D;
    for (int i = 0; i < getSeriesSize(); ++i)
    {
      DataSeries localDataSeries = getSeries(i);
      DataPoint localDataPoint = localDataSeries.getDataPoint(paramInt);
      d += Math.abs(localDataPoint.getValue());
    }
    return d;
  }

  protected double getPercent(int paramInt1, int paramInt2)
  {
    double d = getSeries(paramInt1).getDataPoint(paramInt2).getValue();
    return (Math.abs(d) / getTotalValue(paramInt2));
  }

  public void setStartAngle(int paramInt)
  {
    this.startAngle = paramInt;
  }

  public int getStartAngle()
  {
    return this.startAngle;
  }

  public void setSeparatePercent(int paramInt)
  {
    this.separatePercent = paramInt;
  }

  public int getSeparatePercent()
  {
    return this.separatePercent;
  }

  public void draw(Graphics paramGraphics)
  {
    drawInfo(paramGraphics);
    super.draw(paramGraphics);
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    paramXMLPrintWriter.startTAG("PiePlotGlyph");
    super.writeXML(paramXMLPrintWriter);
    paramXMLPrintWriter.startTAG("Attr").attr("startAngle", this.startAngle).attr("separatePercent", this.separatePercent).end();
    paramXMLPrintWriter.end();
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    super.readXML(paramXMLableReader);
    if (paramXMLableReader.isChildNode())
    {
      String str1 = paramXMLableReader.getTagName();
      if (str1.equals("Attr"))
      {
        String str2;
        if ((str2 = paramXMLableReader.getAttr("startAngle")) != null)
          this.startAngle = Integer.valueOf(str2).intValue();
        if ((str2 = paramXMLableReader.getAttr("separatePercent")) != null)
          this.separatePercent = Integer.valueOf(str2).intValue();
      }
    }
  }

  public boolean equals(Object paramObject)
  {
    if (!(paramObject instanceof PiePlotGlyph))
      return false;
    PiePlotGlyph localPiePlotGlyph = (PiePlotGlyph)paramObject;
    if (!(super.equals(localPiePlotGlyph)))
      return false;
    if (localPiePlotGlyph.startAngle != this.startAngle)
      return false;
    return (localPiePlotGlyph.separatePercent == this.separatePercent);
  }

  public Object clone()
    throws CloneNotSupportedException
  {
    PiePlotGlyph localPiePlotGlyph = (PiePlotGlyph)super.clone();
    return localPiePlotGlyph;
  }

  public JSONObject toJSONObject()
    throws JSONException
  {
    JSONObject localJSONObject = super.toJSONObject();
    localJSONObject.put("startAngle", getStartAngle());
    localJSONObject.put("separatePercent", this.separatePercent);
    return localJSONObject;
  }

  public String getPlotGlyphType()
  {
    return "PiePlotGlyph";
  }
}